import pandas as pd
import numpy as np
import plotly.express as px
import matplotlib.pyplot as plt
print('modules are imported')
import warnings
warnings.filterwarnings('ignore')
modules are imported
dataset_url = 'https://raw.githubusercontent.com/datasets/covid-19/master/data/countries-aggregated.csv'
df = pd.read_csv(dataset_url)
df.head()
| Date | Country | Confirmed | Recovered | Deaths | |
|---|---|---|---|---|---|
| 0 | 2020-01-22 | Afghanistan | 0 | 0 | 0 |
| 1 | 2020-01-23 | Afghanistan | 0 | 0 | 0 |
| 2 | 2020-01-24 | Afghanistan | 0 | 0 | 0 |
| 3 | 2020-01-25 | Afghanistan | 0 | 0 | 0 |
| 4 | 2020-01-26 | Afghanistan | 0 | 0 | 0 |
df.tail()
| Date | Country | Confirmed | Recovered | Deaths | |
|---|---|---|---|---|---|
| 135823 | 2021-12-10 | Zimbabwe | 155817 | 0 | 4723 |
| 135824 | 2021-12-11 | Zimbabwe | 165002 | 0 | 4735 |
| 135825 | 2021-12-12 | Zimbabwe | 167140 | 0 | 4738 |
| 135826 | 2021-12-13 | Zimbabwe | 167140 | 0 | 4738 |
| 135827 | 2021-12-14 | Zimbabwe | 172012 | 0 | 4740 |
df.shape
(135828, 5)
What do you want to do here is to have data from the datethat the first COVID19 confirmed case has been recorded in each of the countries. For doing that, I have to get all of the rows that the confirmed column is more than zero.
df = df[df.Confirmed > 0] #filter data for confirmed covid cases > 0
you can see, I have the data related to each off the countries from the first date that the first confirmed cases has been recorded in those countries.
df.head()
| Date | Country | Confirmed | Recovered | Deaths | |
|---|---|---|---|---|---|
| 33 | 2020-02-24 | Afghanistan | 5 | 0 | 0 |
| 34 | 2020-02-25 | Afghanistan | 5 | 0 | 0 |
| 35 | 2020-02-26 | Afghanistan | 5 | 0 | 0 |
| 36 | 2020-02-27 | Afghanistan | 5 | 0 | 0 |
| 37 | 2020-02-28 | Afghanistan | 5 | 0 | 0 |
df[df.Country == "Italy"] #filter data for Italy
| Date | Country | Confirmed | Recovered | Deaths | |
|---|---|---|---|---|---|
| 58914 | 2020-01-31 | Italy | 2 | 0 | 0 |
| 58915 | 2020-02-01 | Italy | 2 | 0 | 0 |
| 58916 | 2020-02-02 | Italy | 2 | 0 | 0 |
| 58917 | 2020-02-03 | Italy | 2 | 0 | 0 |
| 58918 | 2020-02-04 | Italy | 2 | 0 | 0 |
| ... | ... | ... | ... | ... | ... |
| 59593 | 2021-12-10 | Italy | 5185270 | 0 | 134669 |
| 59594 | 2021-12-11 | Italy | 5206305 | 0 | 134765 |
| 59595 | 2021-12-12 | Italy | 5225517 | 0 | 134831 |
| 59596 | 2021-12-13 | Italy | 5238221 | 0 | 134929 |
| 59597 | 2021-12-14 | Italy | 5258886 | 0 | 135049 |
684 rows × 5 columns
df[df.Country == "Afghanistan"] #filter data for Afghanistan
| Date | Country | Confirmed | Recovered | Deaths | |
|---|---|---|---|---|---|
| 33 | 2020-02-24 | Afghanistan | 5 | 0 | 0 |
| 34 | 2020-02-25 | Afghanistan | 5 | 0 | 0 |
| 35 | 2020-02-26 | Afghanistan | 5 | 0 | 0 |
| 36 | 2020-02-27 | Afghanistan | 5 | 0 | 0 |
| 37 | 2020-02-28 | Afghanistan | 5 | 0 | 0 |
| ... | ... | ... | ... | ... | ... |
| 688 | 2021-12-10 | Afghanistan | 157858 | 0 | 7322 |
| 689 | 2021-12-11 | Afghanistan | 157858 | 0 | 7325 |
| 690 | 2021-12-12 | Afghanistan | 157858 | 0 | 7328 |
| 691 | 2021-12-13 | Afghanistan | 157648 | 0 | 7328 |
| 692 | 2021-12-14 | Afghanistan | 157660 | 0 | 7329 |
660 rows × 5 columns
fig = px.choropleth(df, locations = "Country", locationmode = "country names", color = "Confirmed",
animation_frame = "Date")
fig.update_layout(title_text = "Global Spread of COVID-19 ")
fig.show()
df_china = df[df.Country == "China"]
df_china.head()
| Date | Country | Confirmed | Recovered | Deaths | |
|---|---|---|---|---|---|
| 24948 | 2020-01-22 | China | 548 | 28 | 17 |
| 24949 | 2020-01-23 | China | 643 | 30 | 18 |
| 24950 | 2020-01-24 | China | 920 | 36 | 26 |
| 24951 | 2020-01-25 | China | 1406 | 39 | 42 |
| 24952 | 2020-01-26 | China | 2075 | 49 | 56 |
df_china = df_china[["Date", "Confirmed"]]
df_china.head()
| Date | Confirmed | |
|---|---|---|
| 24948 | 2020-01-22 | 548 |
| 24949 | 2020-01-23 | 643 |
| 24950 | 2020-01-24 | 920 |
| 24951 | 2020-01-25 | 1406 |
| 24952 | 2020-01-26 | 2075 |
Rate of infection = Calculate first derivative of "Confirmed" column
df_china["Infection Rate"] = df_china["Confirmed"].diff()
df_china.head()
| Date | Confirmed | Infection Rate | |
|---|---|---|---|
| 24948 | 2020-01-22 | 548 | NaN |
| 24949 | 2020-01-23 | 643 | 95.0 |
| 24950 | 2020-01-24 | 920 | 277.0 |
| 24951 | 2020-01-25 | 1406 | 486.0 |
| 24952 | 2020-01-26 | 2075 | 669.0 |
#line chart
px.line(df_china, x = "Date", y =["Confirmed","Infection Rate"])
df_china["Infection Rate"].max()
15136.0
df.head()
| Date | Country | Confirmed | Recovered | Deaths | |
|---|---|---|---|---|---|
| 33 | 2020-02-24 | Afghanistan | 5 | 0 | 0 |
| 34 | 2020-02-25 | Afghanistan | 5 | 0 | 0 |
| 35 | 2020-02-26 | Afghanistan | 5 | 0 | 0 |
| 36 | 2020-02-27 | Afghanistan | 5 | 0 | 0 |
| 37 | 2020-02-28 | Afghanistan | 5 | 0 | 0 |
countries = list(df["Country"].unique())
max_infection_rate = []
for c in countries:
#MIR is max_infection_rate
MIR = df[df["Country"]== c].Confirmed.diff().max()
max_infection_rate.append(MIR)
#print(max_infection_rate)
#print(countries)
df_MIR = pd.DataFrame()
df_MIR["Country"] = countries
df_MIR["max_infection_rate"] = max_infection_rate
df_MIR.head()
| Country | max_infection_rate | |
|---|---|---|
| 0 | Afghanistan | 3243.0 |
| 1 | Albania | 1239.0 |
| 2 | Algeria | 1927.0 |
| 3 | Andorra | 696.0 |
| 4 | Angola | 1493.0 |
log_y changes the scale of y axis
px.bar(df_MIR,x = "Country", y = "max_infection_rate", color = "Country", title = "Global Maximum Infection Rate",log_y = "True")
On 9 March 2020, the government of Italy under Prime Minister Giuseppe Conte imposed a national quarantine, restricting the movement of the population except for necessity, work, and health circumstances, in response to the growing pandemic of COVID-19 in the country. source
italy_lockdown_start_date = '2020-03-09'
italy_lockdown_a_month_later = '2020-04-09'
df.head() #all countries with confirmed cases > 0
| Date | Country | Confirmed | Recovered | Deaths | |
|---|---|---|---|---|---|
| 33 | 2020-02-24 | Afghanistan | 5 | 0 | 0 |
| 34 | 2020-02-25 | Afghanistan | 5 | 0 | 0 |
| 35 | 2020-02-26 | Afghanistan | 5 | 0 | 0 |
| 36 | 2020-02-27 | Afghanistan | 5 | 0 | 0 |
| 37 | 2020-02-28 | Afghanistan | 5 | 0 | 0 |
let's get data related to italy
df_italy = df[df.Country == "Italy"]
lets check the dataframe
df_italy.head()
| Date | Country | Confirmed | Recovered | Deaths | |
|---|---|---|---|---|---|
| 58914 | 2020-01-31 | Italy | 2 | 0 | 0 |
| 58915 | 2020-02-01 | Italy | 2 | 0 | 0 |
| 58916 | 2020-02-02 | Italy | 2 | 0 | 0 |
| 58917 | 2020-02-03 | Italy | 2 | 0 | 0 |
| 58918 | 2020-02-04 | Italy | 2 | 0 | 0 |
let's calculate the infection rate in Italy
df_italy["Infection Rate"] = df_italy.Confirmed.diff()
df_italy.head()
| Date | Country | Confirmed | Recovered | Deaths | Infection Rate | |
|---|---|---|---|---|---|---|
| 58914 | 2020-01-31 | Italy | 2 | 0 | 0 | NaN |
| 58915 | 2020-02-01 | Italy | 2 | 0 | 0 | 0.0 |
| 58916 | 2020-02-02 | Italy | 2 | 0 | 0 | 0.0 |
| 58917 | 2020-02-03 | Italy | 2 | 0 | 0 | 0.0 |
| 58918 | 2020-02-04 | Italy | 2 | 0 | 0 | 0.0 |
ok! now let's do the visualization
fig = px.line(df_italy,x = "Date", y = "Infection Rate", title = "Before and after lock down in italy")
fig.add_shape(
dict(
type = "line",
x0 = italy_lockdown_start_date,
y0 = 0,
x1 = italy_lockdown_start_date,
y1 = df_italy["Infection Rate"].max(),
line = dict(color = "Red", width = 2)
)
)
fig.add_annotation(
dict(
x = italy_lockdown_start_date,
y = df_italy["Infection Rate"].max(),
text = "Starting date of lockdown in Italy"
)
)
fig.show()
df_italy.head()
| Date | Country | Confirmed | Recovered | Deaths | Infection Rate | |
|---|---|---|---|---|---|---|
| 58914 | 2020-01-31 | Italy | 2 | 0 | 0 | NaN |
| 58915 | 2020-02-01 | Italy | 2 | 0 | 0 | 0.0 |
| 58916 | 2020-02-02 | Italy | 2 | 0 | 0 | 0.0 |
| 58917 | 2020-02-03 | Italy | 2 | 0 | 0 | 0.0 |
| 58918 | 2020-02-04 | Italy | 2 | 0 | 0 | 0.0 |
let's calculate number of active cases day by day
df_italy["Death Rate"] = df_italy.Deaths.diff()
let's check the dataframe again
df_italy.head()
| Date | Country | Confirmed | Recovered | Deaths | Infection Rate | Death Rate | |
|---|---|---|---|---|---|---|---|
| 58914 | 2020-01-31 | Italy | 2 | 0 | 0 | NaN | NaN |
| 58915 | 2020-02-01 | Italy | 2 | 0 | 0 | 0.0 | 0.0 |
| 58916 | 2020-02-02 | Italy | 2 | 0 | 0 | 0.0 | 0.0 |
| 58917 | 2020-02-03 | Italy | 2 | 0 | 0 | 0.0 | 0.0 |
| 58918 | 2020-02-04 | Italy | 2 | 0 | 0 | 0.0 | 0.0 |
now let's plot a line chart to compare COVID19 national lockdowns impacts on spread of the virus and number of active cases
df_italy["Infection Rate"] = df_italy["Infection Rate"]/df_italy["Infection Rate"].max()
df_italy["Death Rate"] = df_italy["Death Rate"]/df_italy["Death Rate"].max()
fig = px.line(df_italy,x = "Date", y = ["Infection Rate","Death Rate"], title = "Infection rate and death rate in Italy")
fig.show()
from IPython.display import HTML
from IPython.display import Javascript
Javascript("Jupyter.notebook.execute_cells([2])")
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="First Click here to toggle on/off the raw code."></form>''')
import folium
from folium.plugins.measure_control import MeasureControl
#The University of Liverpool - Management School location
m = folium.Map(location=[53.40164,-2.96328], zoom_start=200)
tooltip1 = "The University of Liverpool - Management School"
tooltip2 = "Sydney Jones Library"
folium.Marker(
[53.40164,-2.96328], popup="<i>The University of Liverpool - Management School</i>", tooltip=tooltip1
).add_to(m)
folium.Marker(
[53.40288219796901, -2.9636515174933824], popup="<i>Sydney Jones Library</i>", tooltip=tooltip2
).add_to(m)
m
https://www.express.co.uk/news/uk/1534277/omicron-covid-england-cases-evg
import folium
from folium.features import DivIcon
m1 = folium.Map(
location=[51.37821732201087, -0.0988910151561266],
zoom_start=11,
#tiles='Mapbox Bright'
)
#West Northamptonshire
p1 = [52.25502059017402, -0.8989414297668287]
folium.Marker(p1, icon=DivIcon(
icon_size=(70,19),
icon_anchor=(6,6),
html='<div style="font-size: 9.4pt; color : black">27</div>',
), popup="<i>Northamptonshire</i>").add_to(m1)
m1.add_child(folium.CircleMarker(p1, radius=15))
#Croydon
p2 = [51.37821732201087, -0.0988910151561266]
folium.Marker(p2, icon=DivIcon(
icon_size=(70,19),
icon_anchor=(6,6),
html='<div style="font-size: 9.4pt; color : black">8</div>',
), popup="<i>Croydon</i>").add_to(m1)
m1.add_child(folium.CircleMarker(p2, radius=15))
#Hackney - 8
p3 = [51.57879011625016, -0.07368750825696264]
folium.Marker(p3, icon=DivIcon(
icon_size=(70,19),
icon_anchor=(6,6),
html='<div style="font-size: 9.4pt; color : black">8</div>',
), popup="<i>Hackney</i>").add_to(m1)
m1.add_child(folium.CircleMarker(p3, radius=15))
#Lambeth - 8
p4 = [51.46282053478865, -0.12434179492023828]
folium.Marker(p4, icon=DivIcon(
icon_size=(70,19),
icon_anchor=(6,6),
html='<div style="font-size: 9.4pt; color : black">8</div>',
), popup="<i>Lambeth</i>").add_to(m1)
m1.add_child(folium.CircleMarker(p4, radius=15))
#Newham - 8
p5 = [51.5277484131935, 0.03463148234397976]
folium.Marker(p5, icon=DivIcon(
icon_size=(70,19),
icon_anchor=(6,6),
html='<div style="font-size: 9.4pt; color : black">8</div>',
), popup="<i>Newham</i>").add_to(m1)
m1.add_child(folium.CircleMarker(p5, radius=15))
#Brent - 7
p6 = [51.57649278466461, -0.2706426347485096]
folium.Marker(p6, icon=DivIcon(
icon_size=(70,19),
icon_anchor=(6,6),
html='<div style="font-size: 9.4pt; color : black">7</div>',
), popup="<i>Brent</i>").add_to(m1)
m1.add_child(folium.CircleMarker(p6, radius=15))
#Buckinghamshire - 7
p7 = [51.824810372230566, -0.8108921965299508]
folium.Marker(p7, icon=DivIcon(
icon_size=(70,19),
icon_anchor=(6,6),
html='<div style="font-size: 9.4pt; color : black">7</div>',
), popup="<i>Buckinghamshire</i>").add_to(m1)
m1.add_child(folium.CircleMarker(p7, radius=15))
# Greenwich - 7
p8 = [51.494508660914185, 0.009608469948206529]
folium.Marker(p8, icon=DivIcon(
icon_size=(70,19),
icon_anchor=(6,6),
html='<div style="font-size: 9.4pt; color : black">7</div>',
), popup="<i>Greenwich</i>").add_to(m1)
m1.add_child(folium.CircleMarker(p8, radius=15))
# Lewisham - 7
p9 = [51.46182194198855, -0.007143651078944367]
folium.Marker(p9, icon=DivIcon(
icon_size=(70,19),
icon_anchor=(6,6),
html='<div style="font-size: 9.4pt; color : black">7</div>',
), popup="<i>Lewisham</i>").add_to(m1)
m1.add_child(folium.CircleMarker(p9, radius=15))
# Wandsworth - 6
p10 = [51.46487806381069, -0.18965168767621407]
folium.Marker(p10, icon=DivIcon(
icon_size=(70,19),
icon_anchor=(6,6),
html='<div style="font-size: 9.4pt; color : black">6</div>',
), popup="<i>Wandsworth</i>").add_to(m1)
m1.add_child(folium.CircleMarker(p10, radius=15))
m1